//
//  AllPage.m
//  收纳
//
//  Created by  on 2022/10/26.
//

#import "AllPage.h"
#import "GoodsCell.h"
#import "TypeCell.h"
#import "AddViewController.h"
@interface AllPage()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView2;
@property (nonatomic, strong) NSMutableArray<Goods*>*subArray;
@property (nonatomic, assign) NSInteger index;
@end
@implementation AllPage

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.collectionView registerNib:[GoodsCell nib] forCellWithReuseIdentifier:[GoodsCell ID]];
    [self.collectionView2 registerNib:[TypeCell nib] forCellWithReuseIdentifier:[TypeCell ID]];
}
- (void)loadData:(NSString *)name{
    [GoodsTool find:NO name:name complete:^(NSMutableArray<Goods *> * _Nonnull result) {
        self.subArray = [NSMutableArray arrayWithArray:result];
        [self.collectionView reloadData];
    }];
    [self.collectionView2 reloadData];
}
- (void)setArray:(NSMutableArray<Goods *> *)array{
    _array = array;
    [self loadData:nil];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.collectionView) {
        return self.subArray.count;
    }
    return self.array.count + 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionView) {
        GoodsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[GoodsCell ID] forIndexPath:indexPath];
        cell.model = self.subArray[indexPath.row];
        return  cell;
    }
    TypeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[TypeCell ID] forIndexPath:indexPath];
    cell.title.textHexColor = self.index == indexPath.row ? @"#333333":@"#899094";
    cell.title.text = indexPath.row == 0 ? @"全部" : self.array[indexPath.row - 1].name;
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.collectionView == collectionView) {
        AddViewController *vc = [AddViewController new];
        vc.model = self.subArray[indexPath.row];
        [[self currentVC] push:vc :YES];
        return;
    }
    self.index = indexPath.row;
    [collectionView reloadData];
    [self loadData:indexPath.row == 0 ? nil : self.array[indexPath.row - 1].name];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.collectionView == collectionView) {
        return  CGSizeMake(SCREEN_WIDTH - 40,124);
    }
    NSString *str = indexPath.row == 0 ? @"全部" : self.array[indexPath.row - 1].name;
    CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:16]];
    return  CGSizeMake(ceil(size.width) + 30,44);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (self.collectionView == collectionView) {
        return  UIEdgeInsetsMake(0, 20, 20, 20);
    }
    return  UIEdgeInsetsMake(0, 20, 0, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

@end
